package com.springboot.cloud.sysadmin.organization.rest;

import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.sysadmin.organization.config.Shuguoyiyou;
import com.springboot.cloud.sysadmin.organization.entity.po.ShuFile;
import com.springboot.cloud.sysadmin.organization.file.FileUploadUtils;
import com.springboot.cloud.sysadmin.organization.service.IShuFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Created by ZengJian on 2021/3/12 0012 下午 2:28
 */
@Slf4j
@Api(tags = "文件")
@RestController
@RequestMapping("/shuFile")

public class ShuFileController {
    @Autowired
    private   IShuFileService iShuFileService;

    /**
     * 头像上传
     */
    @ApiOperation(value = "上传文件", notes = "上传文件")
//    @Log(title = "用户头像", businessType = BusinessType.UPDATE)
    @PostMapping("/add")
    public Result avatar(@RequestParam("fileAddress") MultipartFile file) throws IOException {
        ShuFile shuFile = new ShuFile();
        if (!file.isEmpty()) {
            String fileAddress = FileUploadUtils.upload(Shuguoyiyou.getAvatarPath(), file);
            shuFile.setFileAddress(fileAddress);
            return Result.success(iShuFileService.add(shuFile));
        }
        return Result.error("上传文件异常，请联系管理员");
    }


}
